package srms.the512div2;

/**
 * Rock-Paper-Scissors with two hands
 * 
 * @author luh
 */
public class DoubleRoshambo {
	/**
	 * 'R','P','S'
	 * 
	 * @param A
	 * first player Ash's gestures
	 * @param E
	 * second player Elsh's gestures
	 * @return the maximum points Ash could have possibly scored.
	 */
	public int maxScore(String[] A, String[] E) {
		int max = 2;
		final int len = A.length;
		int result = 0;
		int s = 0;
		while (s < len) {
			for (int i = 0; i < len; i++) {
				for (int j = 0; j < len; j++) {
					// System.out.println(A[i] + " " + E[j]);
					if (A[i] != null && E[j] != null) {
						int sum = getScore(A[i], E[j]);
						if (sum == max) {
							result += max;
							A[i] = null;
							E[j] = null;
							s++;
						}
					}
				}
			}
			max--;
		}
		return result;
	}

	private int getScore(String s1, String s2) {
		String leftA = s1.substring(0, 1);
		String rightA = s1.substring(1);
		String leftE = s2.substring(0, 1);
		String rightE = s2.substring(1);
		int result = compare(leftA, leftE) + compare(rightA, rightE);
		if (result < 0)
			return 0;
		return result;
	}

	private int compare(String s1, String s2) {
		if (s1.equals(s2))
			return 0;
		if (s1.compareTo(s2) == 3) {
			return 1;
		}
		if (s1.compareTo(s2) == -3) {
			return -1;
		}
		if (s1.compareTo(s2) < 0)
			return 1;
		return -1;
	}

	public static void main(String[] args) {
		DoubleRoshambo me = new DoubleRoshambo();

		int max = me.maxScore(new String[] { "RR" }, new String[] { "SS" });
		System.out.println(max);

		String[] a = { "SR", "PP" };
		String[] e = { "PR", "RS" };
		max = me.maxScore(a, e);
		System.out.println(max);

		max = me.maxScore(new String[] { "PP", "PP", "PP", "PP" }, new String[] { "RR", "PR", "SS", "SP" });
		System.out.println(max);

		max = me.maxScore(new String[] { "PS", "RS", "PP", "SP", "RR", "SS", "SP" }, new String[] { "RP", "SP", "RR", "SR",
				"RS", "SP", "SS" });
		System.out.println(max);
	}
}
